import { ref } from 'vue'
import { defineStore } from "pinia";
import { post } from "@/utils/request";
import { setCookie,md5 } from "@/utils/utils";
const authStore = defineStore("authStore", () => {
  const userInfo = ref({});
  const setUserInfo = (user) => {
    userInfo.value = user;
  };
  const getUserInfo = () => {
    return userInfo.value;
  };
  const clearUserInfo = () => {
    userInfo.value = {};
  };
  const userlogin = (params,handleSuccess) => {
    post("/login", {
      ...params,
      password: md5(params.password),
    }).then((res) => {
      const { token, username, email, id } = res;
      setCookie("token", token);
      setUserInfo({
        username,
        email,
        id,
      });
      handleSuccess();
    });
  };
  return {
    setUserInfo,
    getUserInfo,
    clearUserInfo,
    userlogin,
  };
});

export default authStore;
